package com.smartstate.iot.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.iot.dto.LogDateDto;
import com.smartstate.iot.dto.PageDTO;
import com.smartstate.iot.entity.TopicConfig;
import com.smartstate.iot.entity.TopicLog;
import com.smartstate.iot.mapper.TopicLogMapper;
import com.smartstate.iot.mapper.TopicMapper;
import com.smartstate.iot.service.TopicLogService;
import com.smartstate.iot.vo.TopicUpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class TopicLogImpl extends ServiceImpl<TopicLogMapper, TopicLog> implements TopicLogService {

    @Autowired
    private TopicLogMapper topicLogMapper;

    @Override
    public Page<TopicLog> queryAllPageByDTO(PageDTO<LogDateDto> pageDTO) {
        Page page = new Page(pageDTO.getPage(), pageDTO.getSize());
        QueryWrapper<TopicLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("date");
        LogDateDto dto = (LogDateDto) pageDTO.getDto();
        if (dto != null) {
            if (dto.getType() != null) {
                queryWrapper.and(wrapper -> wrapper.eq("type", dto.getType()));
            }
            if (dto.getStartTime() != null && dto.getEndTime() != null) {
                queryWrapper.and(wrapper -> wrapper.ge("date", dto.getStartTime()));
                queryWrapper.and(wrapper -> wrapper.le("date", dto.getEndTime()));
            }
        }
        return topicLogMapper.selectPage(page, queryWrapper);
    }

    /**
     * 判断日志新增时是否需要修改以往信息
     *
     * @param content
     * @return
     */

    @Override
    public Boolean getLog(TopicUpVo.Content content) {
        if (topicLogMapper.getLog(content) >= 1) {
            return false;
        }
        return true;
    }


    /**
     * 如果日志有重复的deviceId 就修改信息以往日志名称信息
     *
     * @param topicLog
     * @param content
     * @return
     */

    @Override
    public Boolean updateByDeviceId(TopicLog topicLog, TopicUpVo.Content content) {
        return topicLogMapper.updateByDeviceId(topicLog, content);
    }


//    @Override
//    public List<Integer> findDeviceType() {
//        Integer deviceTypeOK = topicLogMapper.findDeviceTypeOK();
//        Integer deviceTypeNO = topicLogMapper.findDeviceTypeNo();
//        Integer deviceTypeOFF = topicLogMapper.findDeviceTypeOFF();
//        Integer deviceType = topicLogMapper.findDeviceType();
//        ArrayList<Integer> types = new ArrayList<>();
//        types.add(deviceTypeOK);
//        types.add(deviceTypeOFF);
//        types.add(deviceTypeNO);
//        types.add(deviceType);
//        return types;
//    }

    /**
     * 查询同一台盒子上条日志状态是否相同
     * @return
     */
    @Override
    public TopicLog getUpLogType(TopicLog topicLog) {
        TopicLog upLogType = topicLogMapper.getUpLogType(topicLog.getDeviceId());
        System.out.println("upLogType = " + upLogType);
        return upLogType;
    }

    /**
     * 根据deviceId查询盒子状态
     * @param deviceId
     * @return
     */
    @Override
    public TopicLog getTypeByDeviceId(String deviceId) {
        TopicLog topicLog = topicLogMapper.getTypeByDeviceId(deviceId);
        return topicLog;
    }
}
